{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python Exercise -Version Notebook"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- This is the exercise based on the forked repository on GitHub\n",
    "-  Help to improve it if you want, just add the solution under the question\n",
    "- Each notebook only contains 10 questions which is convenient for everyone to locate each question to modify, you can add your own name under the author's list below, let's contribute together !\n",
    "- To visit my personal site: [Click Here](http://yaozeliang.com/resume)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Author's List"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "| Questions | Author       | \n",
    "|:--------:| -------------:|\n",
    "| Original Author | zhiwehu on GitHub |\n",
    "| 11 | Zeliang, your name |\n",
    "| 12 | Zeliang, your name |\n",
    "| 13 | Zeliang, your name |\n",
    "| 14 | Zeliang, your name |\n",
    "| 15 | Zeliang, your name |\n",
    "| 16 | Zeliang, your name |\n",
    "| 17 | Zeliang, your name |\n",
    "| 18 | Zeliang, your name |\n",
    "| 19 | Zeliang, your name |\n",
    "| 20 | Zeliang, your name |"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 11\n",
    "\n",
    "Write a program which accepts a sequence of comma separated 4 digit binary numbers as its input and then check whether they are divisible by 5 or not. The numbers that are divisible by 5 are to be printed in a comma separated sequence.\n",
    "\n",
    "Example:\n",
    "```\n",
    "0100,0011,1010,1001\n",
    "```\n",
    "Then the output should be:\n",
    "```\n",
    "1010\n",
    "```\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1010\n"
     ]
    }
   ],
   "source": [
    "value=[int(x,2) for x in input().split(\",\")]\n",
    "res=\"\"\n",
    "for num in value:\n",
    "    if num%5==0:\n",
    "        if res!=\"\":\n",
    "            res=res+\",\"\n",
    "        res=res+bin(num)[2:]\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 12\n",
    "\n",
    "Write a program, which will find all such numbers between 1000 and 3000 (both included) such that each digit of the number is an even number.\n",
    "The numbers obtained should be printed in a comma-separated sequence on a single line."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2000,2002,2004,2006,2008,2020,2022,2024,2026,2028,2040,2042,2044,2046,2048,2060,2062,2064,2066,2068,2080,2082,2084,2086,2088,2200,2202,2204,2206,2208,2220,2222,2224,2226,2228,2240,2242,2244,2246,2248,2260,2262,2264,2266,2268,2280,2282,2284,2286,2288,2400,2402,2404,2406,2408,2420,2422,2424,2426,2428,2440,2442,2444,2446,2448,2460,2462,2464,2466,2468,2480,2482,2484,2486,2488,2600,2602,2604,2606,2608,2620,2622,2624,2626,2628,2640,2642,2644,2646,2648,2660,2662,2664,2666,2668,2680,2682,2684,2686,2688,2800,2802,2804,2806,2808,2820,2822,2824,2826,2828,2840,2842,2844,2846,2848,2860,2862,2864,2866,2868,2880,2882,2884,2886,2888'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res=[]\n",
    "for num in range(1000,3001):\n",
    "    num = str(num)\n",
    "    if int(num[0])%2==0 and int(num[1])%2==0 and int(num[2])%2==0 and int(num[3])%2==0:\n",
    "        res.append(str(num))\n",
    "res=\",\".join(res)\n",
    "res"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 13\n",
    "\n",
    "Write a program that accepts a sentence and calculate the number of letters and digits.\n",
    "Suppose the following input is supplied to the program:\n",
    "```\n",
    "hello world! 123\n",
    "```\n",
    "Then, the output should be:\n",
    "```\n",
    "LETTERS 10\n",
    "DIGITS 3\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "LETTERS 10\n",
      "DIGITS 3\n"
     ]
    }
   ],
   "source": [
    "value=input().replace(\" \",\"\")\n",
    "num_count=0\n",
    "letter_count=0\n",
    "for chr in value:\n",
    "    if chr.isdigit():\n",
    "        num_count=num_count+1\n",
    "    if chr.isalpha():\n",
    "        letter_count=letter_count+1\n",
    "print(f\"LETTERS {letter_count}\")\n",
    "print(f\"DIGITS {num_count}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 14\n",
    "\n",
    "Write a program that accepts a sentence and calculate the number of upper case letters and lower case letters.\n",
    "Suppose the following input is supplied to the program:\n",
    "```\n",
    "Hello world!\n",
    "```\n",
    "Then, the output should be:\n",
    "```\n",
    "UPPER CASE 1\n",
    "LOWER CASE 9\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "UPPER CASE 1\n",
      "LOWER CASE 9\n"
     ]
    }
   ],
   "source": [
    "value=input().replace(\" \",\"\")\n",
    "lower_count=0\n",
    "upper_count=0\n",
    "for chr in value:\n",
    "    if chr.islower():\n",
    "        lower_count=lower_count+1\n",
    "    if chr.isupper():\n",
    "        upper_count=upper_count+1\n",
    "print(f\"UPPER CASE {upper_count}\")\n",
    "print(f\"LOWER CASE {lower_count}\")\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 15\n",
    "\n",
    "Write a program that computes the value of a+aa+aaa+aaaa with a given digit as the value of a.\n",
    "Suppose the following input is supplied to the program:\n",
    "```\n",
    "9\n",
    "```\n",
    "Then, the output should be:\n",
    "```\n",
    "11106\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11106\n"
     ]
    }
   ],
   "source": [
    "a=int(input())\n",
    "formula=a+int(f\"{a}{a}\")+int(f\"{a}{a}{a}\")+int(f\"{a}{a}{a}{a}\")\n",
    "print(formula)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11106\n"
     ]
    }
   ],
   "source": [
    "res=0\n",
    "a=input()\n",
    "for i in range(1,5):\n",
    "    res=res+int(str(a)*i)\n",
    "print(res)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 16\n",
    "\n",
    "Use a list comprehension to select each odd number in a list. The list is input by a sequence of comma-separated numbers.\n",
    "Suppose the following input is supplied to the program:\n",
    "```\n",
    "1,2,3,4,5,6,7,8,9\n",
    "```\n",
    "Then, the output should be:\n",
    "```\n",
    "1,3,5,7,9\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1,3,5,7,9\n"
     ]
    }
   ],
   "source": [
    "res=[str(x) for x in range(1,10) if x%2 == 1]\n",
    "res=\",\".join(res)\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 17\n",
    "\n",
    "Question:\n",
    "Write a program that computes the net amount of a bank account based a transaction log from console input. The transaction log format is shown as following:\n",
    "```\n",
    "D 100\n",
    "W 200\n",
    "```\n",
    "D means deposit while W means withdrawal.\n",
    "Suppose the following input is supplied to the program:\n",
    "```\n",
    "D 300\n",
    "D 300\n",
    "W 200\n",
    "D 100\n",
    "\n",
    "```\n",
    "Then, the output should be:\n",
    "```\n",
    "500\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "300\n"
     ]
    }
   ],
   "source": [
    "deposit=0\n",
    "while(True):\n",
    "    s=input().split(\" \")\n",
    "    if s==['']:\n",
    "        break\n",
    "    operation1=s[0]\n",
    "    operation2=int(s[1])\n",
    "    if operation1 == \"D\":\n",
    "        deposit=deposit+operation2\n",
    "    elif operation1 == \"W\":\n",
    "        deposit=deposit-operation2\n",
    "    else:\n",
    "        pass\n",
    "print(deposit)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['']"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s=\"\"\n",
    "s=s.split(\" \")\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 18\n",
    "A website requires the users to input username and password to register. Write a program to check the validity of password input by users.\n",
    "Following are the criteria for checking the password:\n",
    "1. At least 1 letter between [a-z]\n",
    "2. At least 1 number between [0-9]\n",
    "1. At least 1 letter between [A-Z]\n",
    "3. At least 1 character from [$#@]\n",
    "4. Minimum length of transaction password: 6\n",
    "5. Maximum length of transaction password: 12\n",
    "\n",
    "Your program should accept a sequence of comma separated passwords and will check them according to the above criteria. Passwords that match the criteria are to be printed, each separated by a comma.\n",
    "\n",
    "If the following passwords are given as input to the program:\n",
    "\n",
    "```\n",
    "ABd1234@1,a F1#,2w3E*,2We3345\n",
    "```\n",
    "Then, the output of the program should be:\n",
    "```\n",
    "ABd1234@1\n",
    "\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['ABd1234@1']"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import re\n",
    "passwds=[x for x in input().split(\",\")]\n",
    "lower_chr=r\"[a-z]\"\n",
    "upper_chr=r\"[A-Z]\"\n",
    "digit=r\"[0-9]\"\n",
    "other_chr=r\"[$#@]\"\n",
    "cor_pwds=[]\n",
    "for pwd in passwds:\n",
    "    if len(pwd)>=6 and len(pwd)<=12:\n",
    "        if re.search(lower_chr,pwd) and re.search(upper_chr,pwd) and re.search(digit,pwd) and re.search(other_chr,pwd):\n",
    "            cor_pwds.append(pwd)\n",
    "cor_pwds"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 19\n",
    "\n",
    "You are required to write a program to sort the (name, age, height) tuples by ascending order where name is string, age and height are numbers. The tuples are input by console. The sort criteria is:\n",
    "1: Sort based on name;\n",
    "2: Then sort based on age;\n",
    "3: Then sort by score.\n",
    "The priority is that name > age > score.\n",
    "If the following tuples are given as input to the program:\n",
    "\n",
    "```\n",
    "Tom,19,80\n",
    "John,20,90\n",
    "Jony,17,91\n",
    "Jony,17,93\n",
    "Json,21,85\n",
    "```\n",
    "Then, the output should be:\n",
    "```\n",
    "[('John', '20', '90'), ('Jony', '17', '91'), ('Jony', '17', '93'), ('Json', '21', '85'), ('Tom', '19', '80')]\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 20\n",
    "\n",
    "Define a class with a generator which can iterate the numbers, which are divisible by 7, between a given range 0 and n."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98]\n"
     ]
    }
   ],
   "source": [
    "class seven_division:\n",
    "    def __init__(self):\n",
    "        pass\n",
    "    def generator(self,n):\n",
    "        res=[]\n",
    "        for i in range(n):\n",
    "            if i%7==0:\n",
    "                res.append(i)\n",
    "        print(res)\n",
    "gen=seven_division()\n",
    "n=int(input())\n",
    "gen.generator(n)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6rc1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
